<?php
/**
*
* @author Blackpawn blackpawn666@hotmail.com - http://blackpawnsarchives.net
* @co_author GameCharmer admin@gamecharmer.com - http://gaming.gamecharmer.com
* @package Age of Dragons Mod :: ( Game Console Required )
* @version $Id: barracks.php 1.1.1 12-26-2009 Blackpawn
* @copyright (c) 2009 Blackpawns Archives
* @license http://opensource.org/licenses/gpl-license.php GNU Public License
*
*/
/**
* @ignore
*/

define('GC_MOD', true);
define('IN_PHPBB', true);
define('AOD', true);

$phpbb_root_path =  '../../';
$templates_path = '../../../games/aod/templates/';
$phpEx = substr(strrchr(__FILE__, '.'), 1);

include($phpbb_root_path . 'common.' . $phpEx);
include($phpbb_root_path . 'includes/functions_display.' . $phpEx);
include($phpbb_root_path . 'includes/mods/game_console/gc_common.' . $phpEx);
include($phpbb_root_path . 'games/aod/common.' . $phpEx);

// Start session management
$user->session_begin();
$auth->acl($user->data);
$user->setup();

// Set custom language path
$user->set_custom_lang_path($phpbb_root_path.'games/aod/language/');

//load our language file
$user->add_lang('common');

// Now add the phpBB language path
$user->set_custom_lang_path($phpbb_root_path . 'language');

// Load the Game Console language file
$user->add_lang('mods/game_console');

if ($user->data['user_id'] == ANONYMOUS)
{
	login_box('', $user->lang['LOGIN']);
}

if ($user->data['is_bot'])
{
	redirect(append_sid("{$phpbb_root_path}index.$phpEx"));
}

// Default Vars
$barracks_page = TRUE; // Highlights the barracks tab
$error_msgs = null;
$admin = 0;
$gold_total = 0;
$food_total = 0;
$gold_total = 0;
$gold_total = 0;

// Var Requests
$cmd = request_var('cmd','');
$training = request_var('training', 0);

// AOD Config
$penalty = ($aod_config['unit_cost_penalty'] / 100);

if($penalty < 0)
{
	$penalty = 0;
}


// Gather some user info
$user_id = $user->data['user_id'];
$account_status = check_account_status($user_id);
if ($account_status == FALSE)
{
	redirect(append_sid("{$phpbb_root_path}/games/index.$phpEx"));
}

// Fetch kingdom data
$king = fetch_kingdom_data($user_id);
$kingdom_status = $king['kingdom_status'];
$admin_access = $king['admin_access'];
$kingdom_sponsor = $king['kingdom_sponsor'];
$kingdom_level = $king['kingdom_level'];
$kingdom_net = $king['kingdom_net'];
$kingdom_land = $king['kingdom_land'];
$kingdom_villagers = $king['kingdom_villagers'];
$kingdom_soldiers = $king['kingdom_soldiers'];
$kingdom_spies = $king['kingdom_spies'];
$kingdom_macemen = $king['kingdom_macemen'];
$kingdom_axemen = $king['kingdom_axemen'];
$kingdom_spearmen = $king['kingdom_spearmen'];
$kingdom_swordsmen = $king['kingdom_swordsmen'];
$kingdom_legionnaires = $king['kingdom_legionnaires'];
$kingdom_knights = $king['kingdom_knights'];
$kingdom_slingers = $king['kingdom_slingers'];
$kingdom_bowmen = $king['kingdom_bowmen'];
$kingdom_long_bowmen = $king['kingdom_long_bowmen'];
$kingdom_crossbowmen = $king['kingdom_crossbowmen'];
$kingdom_light_sword_cavalry = $king['kingdom_light_sword_cavalry'];
$kingdom_heavy_sword_cavalry = $king['kingdom_heavy_sword_cavalry'];
$kingdom_light_archer_cavalry = $king['kingdom_light_archer_cavalry'];
$kingdom_heavy_archer_cavalry = $king['kingdom_heavy_archer_cavalry'];
$kingdom_paladin = $king['kingdom_paladin'];
$kingdom_dragon_knights = $king['kingdom_dragon_knights'];
$kingdom_rams = $king['kingdom_rams'];
$kingdom_catapults = $king['kingdom_catapults'];
$kingdom_heavy_catapults = $king['kingdom_heavy_catapults'];
$kingdom_ballistas = $king['kingdom_ballistas'];
$kingdom_trebuchets = $king['kingdom_trebuchets'];
$kingdom_chimeras = $king['kingdom_chimeras'];
$kingdom_hydras = $king['kingdom_hydras'];
$kingdom_basilisks = $king['kingdom_basilisks'];
$kingdom_wyverns = $king['kingdom_wyverns'];
$kingdom_leviathans = $king['kingdom_leviathans'];
$kingdom_dragons = $king['kingdom_dragons'];
$kingdom_food = $king['kingdom_food'];
$kingdom_wood = $king['kingdom_wood'];
$kingdom_iron = $king['kingdom_iron'];
$kingdom_gold = $king['kingdom_gold'];
$kingdom_stamina = $king['kingdom_stamina'];
$kingdom_turns = $king['kingdom_turns'];
$kingdom_unit_bonus = $king['kingdom_unit_bonus'];
$kingdom_net_penalty = ($kingdom_net * $penalty);

if($kingdom_net_penalty < 0 )
{
	$kingdom_net_penalty = 0;
}

// Check to see if this user is a admin staff member
if ($user_id == 2 || $admin_access == 1)
{
	$admin = 1;
}

// Check kingdom status to see if this account is frozen
if($kingdom_status == 1)
{
	// Lets make sure the main admin can't be blocked
	if ($user_id <> 2){
		trigger_error($user->lang['FROZEN'] . back_link());
	}
}

// Fetch soldier data
$soldier = fetch_unit_data(2);
$soldier_gold = $soldier['unit_gold'];
$soldier_cost = floor(($soldier_gold + $kingdom_net_penalty) - (($soldier_gold + $kingdom_net_penalty) * $kingdom_unit_bonus));

// Cut the price in half for sponsors
if($kingdom_sponsor > 0)
{
	$soldier_cost = floor($soldier_cost / 2);	
}

//commands
if($cmd == $user->lang['TRAIN'])
{
	
	
	
	switch($training)
	{
		case 1:// Train Soldiers

			$amount = ($_POST[2]) ? ereg_replace("[^0-9]", "", $_POST[2]): 0;

			$gold_total = round($amount * $soldier_cost);
			
			if($gold_total > $kingdom_gold)
			{
				$error = TRUE;
				$error_msgs .= $user->lang['NOT_ENOUGH_GOLD'];
			}
			
			if($amount > $kingdom_villagers)
			{
				$error = TRUE;
				$error_msgs .= $user->lang['NOT_ENOUGH_U1'];
			}	
			
			if($amount < 1)
			{
				$error = TRUE;
			}			
			
			if(!$error)
			{
				$db->sql_query(
				"UPDATE " . AOD_KINGDOMS_TABLE ." SET 
				kingdom_villagers = kingdom_villagers -  $amount,
				kingdom_soldiers = kingdom_soldiers +  $amount,
				kingdom_gold = kingdom_gold -  $gold_total
				WHERE user_id = $user_id");
				$db->sql_return_on_error(false);
								
				// Build the report
				$report = sprintf($user->lang['SUCCESSFUL_SOLDIER_TRANS'],number_format($amount),number_format($gold_total));
				
				// Log the report in the database
				report_transaction($user_id,$report,'barracks.php',$date);
				
				// Now it is time to redirect the user to see the report page so they can see the report
				redirect(append_sid("{$phpbb_root_path}/games/aod/report.$phpEx"));				
			}		
		break;
		
		case 2:// Train Advanced Units
			$sql = "SELECT *
			FROM " . AOD_UNITS_TABLE . "
			WHERE unit_level > 0
			ORDER BY id ASC";
			$result = $db->sql_query($sql);
						
			while ($row = $db->sql_fetchrow($result))
			{
				$unit_id = $row['id'];
				$unit[$unit_id]['name'] = $row['unit_name'];
				$unit[$unit_id]['amount'] = ($_POST[$unit_id]) ? ereg_replace("[^0-9]", "", $_POST[$unit_id]): 0;
		

				// Cut the price in half for sponsors
				if($kingdom_sponsor > 0)
				{
					$unit[$unit_id]['gold'] = floor((($unit[$unit_id]['amount'] * ($row['unit_gold'] + $kingdom_net_penalty )) - ( ( $unit[$unit_id]['amount'] * ($row['unit_gold'] + $kingdom_net_penalty)) * $kingdom_unit_bonus)) / 2);
					$unit[$unit_id]['food'] = floor(($unit[$unit_id]['amount'] * $row['unit_food']) / 2);
					$unit[$unit_id]['wood'] = floor(($unit[$unit_id]['amount'] * $row['unit_wood']) / 2);
					$unit[$unit_id]['iron'] = floor(($unit[$unit_id]['amount'] * $row['unit_iron']) / 2);

				}else{
				
					$unit[$unit_id]['gold'] = (($unit[$unit_id]['amount'] * ($row['unit_gold'] + $kingdom_net_penalty )) - ( ( $unit[$unit_id]['amount'] * ($row['unit_gold'] + $kingdom_net_penalty)) * $kingdom_unit_bonus));
					$unit[$unit_id]['food'] = ($unit[$unit_id]['amount'] * $row['unit_food']);
					$unit[$unit_id]['wood'] = ($unit[$unit_id]['amount'] * $row['unit_wood']);
					$unit[$unit_id]['iron'] = ($unit[$unit_id]['amount'] * $row['unit_iron']);
				}
			
				$soldier_total = ($soldier_total +  $unit[$unit_id]['amount']);
				$gold_total = round($gold_total +  $unit[$unit_id]['gold']);
				$food_total = ($food_total +  $unit[$unit_id]['food']);
				$wood_total = ($wood_total +  $unit[$unit_id]['wood']);
				$iron_total = ($iron_total +  $unit[$unit_id]['iron']);
				
			}
			$db->sql_freeresult($result);	
			
			if($gold_total > $kingdom_gold)
			{
				$error = TRUE;
				$error_msgs .= $user->lang['NOT_ENOUGH_GOLD'];
			}
			if($food_total > $kingdom_food)
			{
				$error = TRUE;
				$error_msgs .= $user->lang['NOT_ENOUGH_FOOD'];
			}
			if($wood_total > $kingdom_wood)
			{
				$error = TRUE;
				$error_msgs .= $user->lang['NOT_ENOUGH_WOOD'];
			}
			if($iron_total > $kingdom_iron)
			{
				$error = TRUE;
				$error_msgs .= $user->lang['NOT_ENOUGH_IRON'];
			}
			
			if($soldier_total > $kingdom_soldiers)
			{
				$error = TRUE;
				$error_msgs .= $user->lang['NOT_ENOUGH_U2'];
			}	
			
			if($soldier_total < 1)
			{
				$error = TRUE;
			}			
			
			if(!$error)
			{
				// Spies
				if($unit[3]['amount'] > 0)
				{
					$sql_data[] = "kingdom_spies = kingdom_spies + ".$unit[3]['amount'];
					if($unit[3]['amount'] == 1){
						$unit_msg[0] = $unit[3]['amount'].' Spy';
					}else{
						$unit_msg[0] = number_format($unit[3]['amount']).' Spies';
					}
				}
				
				// Macemen
				if($unit[4]['amount'] > 0)
				{
					$sql_data[] = "kingdom_macemen = kingdom_macemen + ".$unit[4]['amount'];
					if($unit[4]['amount'] == 1){
						$unit_msg[1] = $unit[4]['amount'].' Maceman';
					}else{
						$unit_msg[1] = number_format($unit[4]['amount']).' Macemen';
					}
				}	
				
				// Axemen
				if($unit[5]['amount'] > 0)
				{
					$sql_data[] = "kingdom_axemen = kingdom_axemen + ".$unit[5]['amount'];
					if($unit[5]['amount'] == 1){
						$unit_msg[2] = $unit[5]['amount'].' Axeman';
					}else{
						$unit_msg[2] = number_format($unit[5]['amount']).' Axemen';
					}
				}	
				
				// Spearmen
				if($unit[6]['amount'] > 0)
				{
					$sql_data[] = "kingdom_spearmen = kingdom_spearmen + ".$unit[6]['amount'];
					if($unit[6]['amount'] == 1){
						$unit_msg[3] = $unit[6]['amount'].' Spearman';
					}else{
						$unit_msg[3] = number_format($unit[6]['amount']).' Spearmen';
					}				
				}
				
				// Swordsmen
				if($unit[7]['amount'] > 0)
				{
					$sql_data[] = "kingdom_swordsmen = kingdom_swordsmen + ".$unit[7]['amount'];
					if($unit[7]['amount'] == 1){
						$unit_msg[4] = $unit[7]['amount'].' Swordsman';
					}else{
						$unit_msg[4] = number_format($unit[7]['amount']).' Swordsmen';
					}				
				}				
				
				// Legionnaires
				if($unit[8]['amount'] > 0)
				{
					$sql_data[] = "kingdom_legionnaires = kingdom_legionnaires + ".$unit[8]['amount'];
					if($unit[8]['amount'] == 1){
						$unit_msg[5] = $unit[8]['amount'].' Legionnaire';
					}else{
						$unit_msg[5] = number_format($unit[8]['amount']).' Legionnaires';
					}				
				}					
				
				// Knights
				if($unit[9]['amount'] > 0)
				{
					$sql_data[] = "kingdom_knights = kingdom_knights + ".$unit[9]['amount'];
					if($unit[9]['amount'] == 1){
						$unit_msg[6] = $unit[9]['amount'].' Knight';
					}else{
						$unit_msg[6] = number_format($unit[9]['amount']).' Knights';
					}				
				}	
				
				// Slingers
				if($unit[10]['amount'] > 0)
				{
					$sql_data[] = "kingdom_slingers = kingdom_slingers + ".$unit[10]['amount'];
					if($unit[10]['amount'] == 1){
						$unit_msg[7] = $unit[10]['amount'].' Slinger';
					}else{
						$unit_msg[7] = number_format($unit[10]['amount']).' Slingers';
					}				
				}				
				// Bowmen
				if($unit[11]['amount'] > 0)
				{
					$sql_data[] = "kingdom_bowmen = kingdom_bowmen + ".$unit[11]['amount'];
					if($unit[11]['amount'] == 1){
						$unit_msg[8] = $unit[11]['amount'].' Bowman';
					}else{
						$unit_msg[8] = number_format($unit[11]['amount']).' Bowmen';
					}				
				}

				// Long Bowmen
				if($unit[12]['amount'] > 0)
				{
					$sql_data[] = "kingdom_long_bowmen = kingdom_long_bowmen + ".$unit[12]['amount'];
					if($unit[12]['amount'] == 1){
						$unit_msg[9] = $unit[12]['amount'].' Long Bowman';
					}else{
						$unit_msg[9] = number_format($unit[12]['amount']).' Long Bowmen';
					}				
				}

				// Cross Bowmen
				if($unit[13]['amount'] > 0)
				{
					$sql_data[] = "kingdom_crossbowmen = kingdom_crossbowmen + ".$unit[13]['amount'];
					if($unit[13]['amount'] == 1){
						$unit_msg[10] = $unit[13]['amount'].' Cross Bowman';
					}else{
						$unit_msg[10] = number_format($unit[13]['amount']).' Cross Bowmen';
					}				
				}

				// Light Sword Cavalry
				if($unit[14]['amount'] > 0)
				{
					$sql_data[] = "kingdom_light_sword_cavalry = kingdom_light_sword_cavalry + ".$unit[14]['amount'];
					if($unit[14]['amount'] == 1){
						$unit_msg[11] = $unit[14]['amount'].' Light Sword Cavalry';
					}else{
						$unit_msg[11] = number_format($unit[14]['amount']).' Light Sword Cavalry';
					}				
				}

				// Heavy Sword Cavalry
				if($unit[15]['amount'] > 0)
				{
					$sql_data[] = "kingdom_heavy_sword_cavalry = kingdom_heavy_sword_cavalry + ".$unit[15]['amount'];
					if($unit[15]['amount'] == 1){
						$unit_msg[12] = $unit[15]['amount'].' Heavy Sword Cavalry';
					}else{
						$unit_msg[12] = number_format($unit[15]['amount']).' Heavy Sword Cavalry';
					}				
				}

				// Light Archer Cavalry
				if($unit[16]['amount'] > 0)
				{
					$sql_data[] = "kingdom_light_archer_cavalry = kingdom_light_archer_cavalry + ".$unit[16]['amount'];
					if($unit[16]['amount'] == 1){
						$unit_msg[13] = $unit[16]['amount'].' Light Archer Cavalry';
					}else{
						$unit_msg[13] = number_format($unit[16]['amount']).' Light Archer Cavalry';
					}				
				}

				// Heavy Archer Cavalry
				if($unit[17]['amount'] > 0)
				{
					$sql_data[] = "kingdom_heavy_archer_cavalry = kingdom_heavy_archer_cavalry + ".$unit[17]['amount'];
					if($unit[17]['amount'] == 1){
						$unit_msg[14] = $unit[17]['amount'].' Heavy Archer Cavalry';
					}else{
						$unit_msg[14] = number_format($unit[17]['amount']).' Heavy Archer Cavalry';
					}				
				}

				// Paladin
				if($unit[18]['amount'] > 0)
				{
					$sql_data[] = "kingdom_paladin = kingdom_paladin + ".$unit[18]['amount'];
					if($unit[18]['amount'] == 1){
						$unit_msg[15] = $unit[18]['amount'].' Paladin';
					}else{
						$unit_msg[15] = number_format($unit[18]['amount']).' Paladins';
					}				
				}

				// Dragon Knights
				if($unit[19]['amount'] > 0)
				{
					$sql_data[] = "kingdom_dragon_knights = kingdom_dragon_knights + ".$unit[19]['amount'];
					if($unit[19]['amount'] == 1){
						$unit_msg[16] = $unit[19]['amount'].' Dragon Knight';
					}else{
						$unit_msg[16] = number_format($unit[19]['amount']).' Dragon Knights';
					}				
				}

				// Rams
				if($unit[20]['amount'] > 0)
				{
					$sql_data[] = "kingdom_rams = kingdom_rams + ".$unit[20]['amount'];
					if($unit[20]['amount'] == 1){
						$unit_msg[17] = $unit[20]['amount'].' Ram';
					}else{
						$unit_msg[17] = number_format($unit[20]['amount']).' Rams';
					}				
				}				

				// Catapults
				if($unit[21]['amount'] > 0)
				{
					$sql_data[] = "kingdom_catapults = kingdom_catapults + ".$unit[21]['amount'];
					if($unit[21]['amount'] == 1){
						$unit_msg[18] = $unit[21]['amount'].' Catapult';
					}else{
						$unit_msg[18] = number_format($unit[21]['amount']).' Catapults';
					}				
				}

				// Heavy Catapults
				if($unit[22]['amount'] > 0)
				{
					$sql_data[] = "kingdom_heavy_catapults = kingdom_heavy_catapults + ".$unit[22]['amount'];
					if($unit[22]['amount'] == 1){
						$unit_msg[19] = $unit[22]['amount'].' Heavy Catapult';
					}else{
						$unit_msg[19] = number_format($unit[22]['amount']).' Heavy Catapults';
					}				
				}

				// Ballistas
				if($unit[23]['amount'] > 0)
				{
					$sql_data[] = "kingdom_ballistas = kingdom_ballistas + ".$unit[23]['amount'];
					if($unit[23]['amount'] == 1){
						$unit_msg[20] = $unit[23]['amount'].' Ballista';
					}else{
						$unit_msg[20] = number_format($unit[23]['amount']).' Ballistas';
					}				
				}

				// Trebuchets
				if($unit[24]['amount'] > 0)
				{
					$sql_data[] = "kingdom_trebuchets = kingdom_trebuchets + ".$unit[24]['amount'];
					if($unit[24]['amount'] == 1){
						$unit_msg[21] = $unit[24]['amount'].' Trebuchet';
					}else{
						$unit_msg[21] = number_format($unit[24]['amount']).' Trebuchets';
					}				
				}

				// Chimeras
				if($unit[25]['amount'] > 0)
				{
					$sql_data[] = "kingdom_chimeras = kingdom_chimeras + ".$unit[25]['amount'];
					if($unit[25]['amount'] == 1){
						$unit_msg[22] = $unit[25]['amount'].' Chimera';
					}else{
						$unit_msg[22] = number_format($unit[25]['amount']).' Chimeras';
					}				
				}

				// Hydras
				if($unit[26]['amount'] > 0)
				{
					$sql_data[] = "kingdom_hydras = kingdom_hydras + ".$unit[26]['amount'];
					if($unit[26]['amount'] == 1){
						$unit_msg[23] = $unit[26]['amount'].' Hydra';
					}else{
						$unit_msg[23] = number_format($unit[26]['amount']).' Hydras';
					}				
				}

				// Basilisks
				if($unit[27]['amount'] > 0)
				{
					$sql_data[] = "kingdom_basilisks = kingdom_basilisks + ".$unit[27]['amount'];
					if($unit[27]['amount'] == 1){
						$unit_msg[24] = $unit[27]['amount'].' Basilisk';
					}else{
						$unit_msg[24] = number_format($unit[27]['amount']).' Basilisks';
					}				
				}

				// Wyverns
				if($unit[28]['amount'] > 0)
				{
					$sql_data[] = "kingdom_wyverns = kingdom_wyverns + ".$unit[28]['amount'];
					if($unit[28]['amount'] == 1){
						$unit_msg[25] = $unit[28]['amount'].' Wyvern';
					}else{
						$unit_msg[25] = number_format($unit[28]['amount']).' Wyverns';
					}				
				}

				// Leviathans
				if($unit[29]['amount'] > 0)
				{
					$sql_data[] = "kingdom_leviathans = kingdom_leviathans + ".$unit[29]['amount'];
					if($unit[29]['amount'] == 1){
						$unit_msg[26] = $unit[29]['amount'].' Leviathan';
					}else{
						$unit_msg[26] = number_format($unit[29]['amount']).' Leviathans';
					}				
				}

				// Dragons
				if($unit[30]['amount'] > 0)
				{
					$sql_data[] = "kingdom_dragons = kingdom_dragons + ".$unit[30]['amount'];
					if($unit[30]['amount'] == 1){
						$unit_msg[27] = $unit[30]['amount'].' Dragon';
					}else{
						$unit_msg[27] = number_format($unit[30]['amount']).' Dragons';
					}				
				}				
											
				// Build SQL data list
				$sql_data = build_sql_list($sql_data);
				$unit_msg = build_list($unit_msg);
				
				$db->sql_query(
				"UPDATE " . AOD_KINGDOMS_TABLE ." SET 
				kingdom_soldiers = kingdom_soldiers - $soldier_total,
				kingdom_gold = kingdom_gold - $gold_total,
				kingdom_food = kingdom_food - $food_total,
				kingdom_wood = kingdom_wood - $wood_total,
				kingdom_iron = kingdom_iron - $iron_total,
				$sql_data
				WHERE user_id = $user_id");
				$db->sql_return_on_error(false);

				
				// Build the report
				$report = sprintf($user->lang['SUCCESSFUL_UNIT_TRAIN_1'],number_format($soldier_total)).$unit_msg.sprintf($user->lang['SUCCESSFUL_UNIT_TRAIN_2'],number_format($gold_total),number_format($food_total),number_format($wood_total),number_format($iron_total));
				
				// Log the report in the database
				report_transaction($user_id,$report,'barracks.php',$date);
				
				// Now it is time to redirect the user to see the report page so they can see the report
				redirect(append_sid("{$phpbb_root_path}/games/aod/report.$phpEx"));					
			}
		break;
	}

}

if($cmd == $user->lang['UNTRAIN'])
{
	// Untrain
	$sql = "SELECT *
	FROM " . AOD_UNITS_TABLE . "
	WHERE unit_level > 0
	ORDER BY id ASC";
	$result = $db->sql_query($sql);
				
	while ($row = $db->sql_fetchrow($result))
	{
		$unit_id = $row['id'];
		$unit[$unit_id]['name'] = $row['unit_name'];
		$unit[$unit_id]['amount'] = ($_POST[$unit_id]) ? ereg_replace("[^0-9]", "", $_POST[$unit_id]): 0;

		// Cut the price in half for sponsors
		if($kingdom_sponsor > 0)
		{
			$unit[$unit_id]['gold'] = floor((($unit[$unit_id]['amount'] * ($row['unit_gold'] + $kingdom_net_penalty )) - ( ( $unit[$unit_id]['amount'] * ($row['unit_gold'] + $kingdom_net_penalty)) * $kingdom_unit_bonus)) / 2);
			$unit[$unit_id]['food'] = floor(($unit[$unit_id]['amount'] * $row['unit_food']) / 2);
			$unit[$unit_id]['wood'] = floor(($unit[$unit_id]['amount'] * $row['unit_wood']) / 2);
			$unit[$unit_id]['iron'] = floor(($unit[$unit_id]['amount'] * $row['unit_iron']) / 2);

		}else{
		
			$unit[$unit_id]['gold'] = (($unit[$unit_id]['amount'] * ($row['unit_gold'] + $kingdom_net_penalty )) - ( ( $unit[$unit_id]['amount'] * ($row['unit_gold'] + $kingdom_net_penalty)) * $kingdom_unit_bonus));
			$unit[$unit_id]['food'] = ($unit[$unit_id]['amount'] * $row['unit_food']);
			$unit[$unit_id]['wood'] = ($unit[$unit_id]['amount'] * $row['unit_wood']);
			$unit[$unit_id]['iron'] = ($unit[$unit_id]['amount'] * $row['unit_iron']);
		}

		$soldier_total = ($soldier_total +  $unit[$unit_id]['amount']);
		$gold_total = round($gold_total +  $unit[$unit_id]['gold']);
		$food_total = ($food_total +  $unit[$unit_id]['food']);
		$wood_total = ($wood_total +  $unit[$unit_id]['wood']);
		$iron_total = ($iron_total +  $unit[$unit_id]['iron']);
		
	}
	$db->sql_freeresult($result);
	
	$gold_total = floor($gold_total / 2);
	$food_total = floor($food_total / 2);
	$wood_total = floor($wood_total / 2);
	$iron_total = floor($iron_total / 2);
		
	// Spies
	if($unit[3]['amount'] > $kingdom_spies)
	{
		
		$error = TRUE;
		$error_msgs .= $user->lang['NOT_ENOUGH_U3'];
		
	}else{
		
		$sql_data[] = "kingdom_spies = kingdom_spies - ".$unit[3]['amount'];
		
		if($unit[3]['amount'] == 1){
			$unit_msg[0] = $unit[3]['amount'].' Spy';
		}
		
		if($unit[3]['amount'] > 1){
			$unit_msg[0] = number_format($unit[3]['amount']).' Spies';
		}	
			
	}

	// Macemen
	if($unit[4]['amount'] > $kingdom_macemen)
	{
		
		$error = TRUE;
		$error_msgs .= $user->lang['NOT_ENOUGH_U4'];
		
	}else{
				
		$sql_data[] = "kingdom_macemen = kingdom_macemen - ".$unit[4]['amount'];
		
		if($unit[4]['amount'] == 1){
			$unit_msg[1] = $unit[4]['amount'].' Maceman';
		}
		
		if($unit[4]['amount'] > 1){
			$unit_msg[1] = number_format($unit[4]['amount']).' Macemen';
		}
	}	

	// Axemen
	if($unit[5]['amount'] > $kingdom_axemen)
	{
		
		$error = TRUE;
		$error_msgs .= $user->lang['NOT_ENOUGH_U5'];
		
	}else{
				
		$sql_data[] = "kingdom_axemen = kingdom_axemen - ".$unit[5]['amount'];
		
		if($unit[5]['amount'] == 1){
			$unit_msg[2] = $unit[5]['amount'].' Axeman';
		}
		
		if($unit[5]['amount'] > 1){
			$unit_msg[2] = number_format($unit[5]['amount']).' Axemen';
		}
	}	
				
	// Spearmen
	if($unit[6]['amount'] > $kingdom_spearmen)
	{
		
		$error = TRUE;
		$error_msgs .= $user->lang['NOT_ENOUGH_U6'];
		
	}else{
				
		$sql_data[] = "kingdom_spearmen = kingdom_spearmen - ".$unit[6]['amount'];
		
		if($unit[6]['amount'] == 1){
			$unit_msg[3] = $unit[6]['amount'].' Spearman';
		}
		
		if($unit[6]['amount'] > 1){
			$unit_msg[3] = number_format($unit[6]['amount']).' Spearmen';
		}				
	}
				
	// Swordsmen
	if($unit[7]['amount'] > $kingdom_swordsmen)
	{
		
		$error = TRUE;
		$error_msgs .= $user->lang['NOT_ENOUGH_U7'];
		
	}else{
				
		$sql_data[] = "kingdom_swordsmen = kingdom_swordsmen - ".$unit[7]['amount'];

		if($unit[7]['amount'] == 1){
			$unit_msg[4] = $unit[7]['amount'].' Swordsman';
		}
		
		if($unit[7]['amount'] > 1){
			$unit_msg[4] = number_format($unit[7]['amount']).' Swordsmen';
		}				
	}				
				
	// Legionnaires
	if($unit[8]['amount'] > $kingdom_legionnaires)
	{
		
		$error = TRUE;
		$error_msgs .= $user->lang['NOT_ENOUGH_U8'];
		
	}else{
				
		$sql_data[] = "kingdom_legionnaires = kingdom_legionnaires - ".$unit[8]['amount'];

		if($unit[8]['amount'] == 1){
			$unit_msg[5] = $unit[8]['amount'].' Legionnaire';
		}
		
		if($unit[8]['amount'] > 1){
			$unit_msg[5] = number_format($unit[8]['amount']).' Legionnaires';
		}				
	}					
				
	// Knights
	if($unit[9]['amount'] > $kingdom_knights)
	{
		
		$error = TRUE;
		$error_msgs .= $user->lang['NOT_ENOUGH_U9'];
		
	}else{
				
		$sql_data[] = "kingdom_knights = kingdom_knights - ".$unit[9]['amount'];
		
		if($unit[9]['amount'] == 1){
			$unit_msg[6] = $unit[9]['amount'].' Knight';
		}
		
		if($unit[9]['amount'] > 1){
			$unit_msg[6] = number_format($unit[9]['amount']).' Knights';
		}				
	}	
				
	// Slingers
	if($unit[10]['amount'] > $kingdom_slingers)
	{
		
		$error = TRUE;
		$error_msgs .= $user->lang['NOT_ENOUGH_U10'];
		
	}else{
				
		$sql_data[] = "kingdom_slingers = kingdom_slingers - ".$unit[10]['amount'];
		
		if($unit[10]['amount'] == 1){
			$unit_msg[7] = $unit[10]['amount'].' Slinger';
		}
		
		if($unit[10]['amount'] > 1){
			$unit_msg[7] = number_format($unit[10]['amount']).' Slingers';
		}				
	}	
							
	// Bowmen
	if($unit[11]['amount'] > $kingdom_bowmen)
	{
		
		$error = TRUE;
		$error_msgs .= $user->lang['NOT_ENOUGH_U11'];
		
	}else{
				
		$sql_data[] = "kingdom_bowmen = kingdom_bowmen - ".$unit[11]['amount'];
		
		if($unit[11]['amount'] == 1){
			$unit_msg[8] = $unit[11]['amount'].' Bowman';
		}
		
		if($unit[11]['amount'] > 1){
			$unit_msg[8] = number_format($unit[11]['amount']).' Bowmen';
		}				
	}

	// Long Bowmen
	if($unit[12]['amount'] > $kingdom_long_bowmen)
	{
		
		$error = TRUE;
		$error_msgs .= $user->lang['NOT_ENOUGH_U12'];
		
	}else{
				
		$sql_data[] = "kingdom_long_bowmen = kingdom_long_bowmen - ".$unit[12]['amount'];
		
		if($unit[12]['amount'] == 1){
			$unit_msg[9] = $unit[12]['amount'].' Long Bowman';
		}
		
		if($unit[12]['amount'] > 1){
			$unit_msg[9] = number_format($unit[12]['amount']).' Long Bowmen';
		}				
	}

	// Cross Bowmen
	if($unit[13]['amount'] > $kingdom_crossbowmen)
	{
		
		$error = TRUE;
		$error_msgs .= $user->lang['NOT_ENOUGH_U13'];
		
	}else{
				
		$sql_data[] = "kingdom_crossbowmen = kingdom_crossbowmen - ".$unit[13]['amount'];
		
		if($unit[13]['amount'] == 1){
			$unit_msg[10] = $unit[13]['amount'].' Cross Bowman';
		}
		
		if($unit[13]['amount'] > 1){
			$unit_msg[10] = number_format($unit[13]['amount']).' Cross Bowmen';
		}				
	}

	// Light Sword Cavalry
	if($unit[14]['amount'] > $kingdom_light_sword_cavalry)
	{
		
		$error = TRUE;
		$error_msgs .= $user->lang['NOT_ENOUGH_U14'];
		
	}else{
				
		$sql_data[] = "kingdom_light_sword_cavalry = kingdom_light_sword_cavalry - ".$unit[14]['amount'];
		
		if($unit[14]['amount'] == 1){
			$unit_msg[11] = $unit[14]['amount'].' Light Sword Cavalry';
		}
		
		if($unit[14]['amount'] > 1){
			$unit_msg[11] = number_format($unit[14]['amount']).' Light Sword Cavalry';
		}				
	}

	// Heavy Sword Cavalry
	if($unit[15]['amount'] > $kingdom_heavy_sword_cavalry)
	{
		
		$error = TRUE;
		$error_msgs .= $user->lang['NOT_ENOUGH_U15'];
		
	}else{
				
		$sql_data[] = "kingdom_heavy_sword_cavalry = kingdom_heavy_sword_cavalry - ".$unit[15]['amount'];
		
		if($unit[15]['amount'] == 1){
			$unit_msg[12] = $unit[15]['amount'].' Heavy Sword Cavalry';
		}
		
		if($unit[15]['amount'] > 1){
			$unit_msg[12] = number_format($unit[15]['amount']).' Heavy Sword Cavalry';
		}				
	}

	// Light Archer Cavalry
	if($unit[16]['amount'] > $kingdom_light_archer_cavalry)
	{
		
		$error = TRUE;
		$error_msgs .= $user->lang['NOT_ENOUGH_U16'];
		
	}else{
				
		$sql_data[] = "kingdom_light_archer_cavalry = kingdom_light_archer_cavalry - ".$unit[16]['amount'];
		
		if($unit[16]['amount'] == 1){
			$unit_msg[13] = $unit[16]['amount'].' Light Archer Cavalry';
		}
		
		if($unit[16]['amount'] > 1){
			$unit_msg[13] = number_format($unit[16]['amount']).' Light Archer Cavalry';
		}				
	}

	// Heavy Archer Cavalry
	if($unit[17]['amount'] > $kingdom_heavy_archer_cavalry)
	{
		
		$error = TRUE;
		$error_msgs .= $user->lang['NOT_ENOUGH_U17'];
		
	}else{
				
		$sql_data[] = "kingdom_heavy_archer_cavalry = kingdom_heavy_archer_cavalry - ".$unit[17]['amount'];
		
		if($unit[17]['amount'] == 1){
			$unit_msg[14] = $unit[17]['amount'].' Heavy Archer Cavalry';
		}
		
		if($unit[17]['amount'] > 1){
			$unit_msg[14] = number_format($unit[17]['amount']).' Heavy Archer Cavalry';
		}				
	}

	// Paladin
	if($unit[18]['amount'] > $kingdom_paladin)
	{
		
		$error = TRUE;
		$error_msgs .= $user->lang['NOT_ENOUGH_U18'];
		
	}else{
				
		$sql_data[] = "kingdom_paladin = kingdom_paladin - ".$unit[18]['amount'];
		
		if($unit[18]['amount'] == 1){
			$unit_msg[15] = $unit[18]['amount'].' Paladin';
		}
		
		if($unit[18]['amount'] > 1){
			$unit_msg[15] = number_format($unit[18]['amount']).' Paladins';
		}				
	}

	// Dragon Knights
	if($unit[19]['amount'] > $kingdom_dragon_knights)
	{
		
		$error = TRUE;
		$error_msgs .= $user->lang['NOT_ENOUGH_U19'];
		
	}else{
				
		$sql_data[] = "kingdom_dragon_knights = kingdom_dragon_knights - ".$unit[19]['amount'];
		
		if($unit[19]['amount'] == 1){
			$unit_msg[16] = $unit[19]['amount'].' Dragon Knight';
		}
		
		if($unit[19]['amount'] > 1){
			$unit_msg[16] = number_format($unit[19]['amount']).' Dragon Knights';
		}				
	}

	// Rams
	if($unit[20]['amount'] > $kingdom_rams)
	{
		
		$error = TRUE;
		$error_msgs .= $user->lang['NOT_ENOUGH_U20'];
		
	}else{
				
		$sql_data[] = "kingdom_rams = kingdom_rams - ".$unit[20]['amount'];
		
		if($unit[20]['amount'] == 1){
			$unit_msg[17] = $unit[20]['amount'].' Ram';
		}
		
		if($unit[20]['amount'] > 1){
			$unit_msg[17] = number_format($unit[20]['amount']).' Rams';
		}				
	}				

	// Catapults
	if($unit[21]['amount'] > $kingdom_catapults)
	{
		
		$error = TRUE;
		$error_msgs .= $user->lang['NOT_ENOUGH_U21'];
		
	}else{
				
		$sql_data[] = "kingdom_catapults = kingdom_catapults - ".$unit[21]['amount'];
		
		if($unit[21]['amount'] == 1){
			$unit_msg[18] = $unit[21]['amount'].' Catapult';
		}
		
		if($unit[21]['amount'] > 1){
			$unit_msg[18] = number_format($unit[21]['amount']).' Catapults';
		}				
	}

	// Heavy Catapults
	if($unit[22]['amount'] > $kingdom_heavy_catapults)
	{
		
		$error = TRUE;
		$error_msgs .= $user->lang['NOT_ENOUGH_U22'];
		
	}else{
				
		$sql_data[] = "kingdom_heavy_catapults = kingdom_heavy_catapults - ".$unit[22]['amount'];
		
		if($unit[22]['amount'] == 1){
			$unit_msg[19] = $unit[22]['amount'].' Heavy Catapult';
		}
		
		if($unit[22]['amount'] > 1){
			$unit_msg[19] = number_format($unit[22]['amount']).' Heavy Catapults';
		}				
	}

	// Ballistas
	if($unit[23]['amount'] > $kingdom_ballistas)
	{
		
		$error = TRUE;
		$error_msgs .= $user->lang['NOT_ENOUGH_U23'];
		
	}else{
				
		$sql_data[] = "kingdom_ballistas = kingdom_ballistas - ".$unit[23]['amount'];
		
		if($unit[23]['amount'] == 1){
			$unit_msg[20] = $unit[23]['amount'].' Ballista';
		}
		
		if($unit[23]['amount'] > 1){
			$unit_msg[20] = number_format($unit[23]['amount']).' Ballistas';
		}				
	}

	// Trebuchets
	if($unit[24]['amount'] > $kingdom_trebuchets)
	{
		
		$error = TRUE;
		$error_msgs .= $user->lang['NOT_ENOUGH_U24'];
		
	}else{
				
		$sql_data[] = "kingdom_trebuchets = kingdom_trebuchets - ".$unit[24]['amount'];
		
		if($unit[24]['amount'] == 1){
			$unit_msg[21] = $unit[24]['amount'].' Trebuchet';
		}
		
		if($unit[24]['amount'] > 1){
			$unit_msg[21] = number_format($unit[24]['amount']).' Trebuchets';
		}				
	}

	// Chimeras
	if($unit[25]['amount'] > $kingdom_chimeras)
	{
		
		$error = TRUE;
		$error_msgs .= $user->lang['NOT_ENOUGH_U25'];
		
	}else{
				
		$sql_data[] = "kingdom_chimeras = kingdom_chimeras - ".$unit[25]['amount'];
		
		if($unit[25]['amount'] == 1){
			$unit_msg[22] = $unit[25]['amount'].' Chimera';
		}
		
		if($unit[25]['amount'] > 1){
			$unit_msg[22] = number_format($unit[25]['amount']).' Chimeras';
		}				
	}

	// Hydras
	if($unit[26]['amount'] > $kingdom_hydras)
	{
		
		$error = TRUE;
		$error_msgs .= $user->lang['NOT_ENOUGH_U26'];
		
	}else{
				
		$sql_data[] = "kingdom_hydras = kingdom_hydras - ".$unit[26]['amount'];
		
		if($unit[26]['amount'] == 1){
			$unit_msg[23] = $unit[26]['amount'].' Hydra';
		}
		
		if($unit[26]['amount'] > 1){
			$unit_msg[23] = number_format($unit[26]['amount']).' Hydras';
		}				
	}

	// Basilisks
	if($unit[27]['amount'] > $kingdom_basilisks)
	{
		
		$error = TRUE;
		$error_msgs .= $user->lang['NOT_ENOUGH_U27'];
		
	}else{
				
		$sql_data[] = "kingdom_basilisks = kingdom_basilisks - ".$unit[27]['amount'];
		
		if($unit[27]['amount'] == 1){
			$unit_msg[24] = $unit[27]['amount'].' Basilisk';
		}
		
		if($unit[27]['amount'] > 1){
			$unit_msg[24] = number_format($unit[27]['amount']).' Basilisks';
		}				
	}

	// Wyverns
	if($unit[28]['amount'] > $kingdom_wyverns)
	{
		
		$error = TRUE;
		$error_msgs .= $user->lang['NOT_ENOUGH_U28'];
		
	}else{
				
		$sql_data[] = "kingdom_wyverns = kingdom_wyverns - ".$unit[28]['amount'];
		
		if($unit[28]['amount'] == 1){
			$unit_msg[25] = $unit[28]['amount'].' Wyvern';
		}
		
		if($unit[28]['amount'] > 1){
			$unit_msg[25] = number_format($unit[28]['amount']).' Wyverns';
		}				
	}

	// Leviathans
	if($unit[29]['amount'] > $kingdom_leviathans)
	{
		
		$error = TRUE;
		$error_msgs .= $user->lang['NOT_ENOUGH_U29'];
		
	}else{
				
		$sql_data[] = "kingdom_leviathans = kingdom_leviathans - ".$unit[29]['amount'];
		
		if($unit[29]['amount'] == 1){
			$unit_msg[26] = $unit[29]['amount'].' Leviathan';
		}
		
		if($unit[29]['amount'] > 1){
			$unit_msg[26] = number_format($unit[29]['amount']).' Leviathans';
		}				
	}

	// Dragons
	if($unit[30]['amount'] > $kingdom_dragons)
	{
		
		$error = TRUE;
		$error_msgs .= $user->lang['NOT_ENOUGH_U30'].'ERROR:';
		
	}else{
				
		$sql_data[] = "kingdom_dragons = kingdom_dragons - ".$unit[30]['amount'];
		
		if($unit[30]['amount'] == 1){
			$unit_msg[27] = $unit[30]['amount'].' Dragon';
		}
		
		if($unit[30]['amount'] > 1){
			$unit_msg[27] = number_format($unit[30]['amount']).' Dragons';
		}				
	}				
						
	if(!$error)
	{
		// Build SQL data list
		$sql_data = build_sql_list($sql_data);
		$unit_msg = build_list($unit_msg);
		
		$db->sql_query(
		"UPDATE " . AOD_KINGDOMS_TABLE ." SET 
		kingdom_soldiers = kingdom_soldiers + $soldier_total,
		kingdom_gold = kingdom_gold + $gold_total,
		kingdom_food = kingdom_food + $food_total,
		kingdom_wood = kingdom_wood + $wood_total,
		kingdom_iron = kingdom_iron + $iron_total,
		$sql_data
		WHERE user_id = $user_id");
		$db->sql_return_on_error(false);
		
		// Build the report
		$report = $user->lang['SUCCESSFUL_UNIT_UNTRAIN_1'].$unit_msg.sprintf($user->lang['SUCCESSFUL_UNIT_UNTRAIN_2'] , number_format($gold_total) , number_format($food_total) , number_format($wood_total) , number_format($iron_total));
		
		// Log the report in the database
		report_transaction($user_id,$report,'barracks.php',$date);
		
		// Now it is time to redirect the user to see the report page so they can see the report
		redirect(append_sid("{$phpbb_root_path}/games/aod/report.$phpEx"));			
	}		
}

$unitcount = fetch_kingdom_unit_counts($user_id);

// Build unit list	
$sql = "SELECT *
FROM " . AOD_UNITS_TABLE . "
WHERE unit_level <= $kingdom_level
AND unit_level > 0

ORDER BY id ASC";
$result = $db->sql_query($sql);
			
while ($row = $db->sql_fetchrow($result))
{

	$uid = $row['id'];
	$unit_name = $row['unit_name'];
	$unit_net = $row['unit_net'];
	$unit_off = $row['unit_off'];
	$unit_def = $row['unit_def'];
	$unit_gold = $row['unit_gold'];
	$unit_food = $row['unit_food'];
	$unit_wood = $row['unit_wood'];
	$unit_iron = $row['unit_iron'];

	$unit_gold = (($unit_gold + $kingdom_net_penalty) - (($unit_gold + $kingdom_net_penalty) * $kingdom_unit_bonus));

	// Cut the price in half for sponsors
	if($kingdom_sponsor > 0)
	{
		$unit_gold = floor($unit_gold / 2);	
		$unit_food = floor($unit_food / 2);	
		$unit_wood = floor($unit_wood / 2);	
		$unit_iron = floor($unit_iron / 2);	
	}
	$value=NULL;
	
	$gold_max = floor($kingdom_gold / $unit_gold);
	$value[0] = $gold_max;
	
	if($unit_food > 0){
		$food_max = floor($kingdom_food / $unit_food);
		$value[1] = $food_max; 
	}
	
	if($unit_wood > 0){
		$wood_max = floor($kingdom_wood / $unit_wood);
		$value[2] = $wood_max; 
	}

	if($unit_iron > 0){
		$iron_max = floor($kingdom_iron / $unit_iron);
		$value[3] = $iron_max; 
	}
	
	$value[4] = $kingdom_soldiers;

	$max_train = NULL;
	$max_train = min($value);
			
	$template->assign_block_vars('unitrow', array(
		'UNAME'		=> $unit_name,
		'UNET'		=> number_format($unit_net),
		'UOFF'		=> number_format($unit_off),
		'UDEF'		=> number_format($unit_def),
		'UGOLD'		=> number_format($unit_gold),
		'UFOOD'		=> number_format($unit_food),
		'UWOOD'		=> number_format($unit_wood),
		'UIRON'		=> number_format($unit_iron),
		'UOWN'		=> number_format($unitcount[$uid]),
		'UMAX'		=> number_format($max_train),
		'UAMOUNT'	=> '<input type="text" class="game_post" maxlength="8" name="'.$uid.'">',		
		)
	);	
}	
$db->sql_freeresult($result);

// Display an error messages to the user
if($error_msgs){
	$error_msgs = '<span class="errormsg"><b>'.$error_msgs.'</b></span>';
}

$gold_max = floor($kingdom_gold / $soldier_cost);
$max_train = min($kingdom_villagers,$gold_max);

include($phpbb_root_path . 'games/aod/includes/page_header.' . $phpEx);

$template->assign_vars(array(
	'SOLDIER'				=> sprintF($user->lang['SOLDIER_TRAIN_EXPLAIN'] ,number_format($kingdom_villagers), number_format($soldier_cost)),
	'MAX_SOLDIERS'			=> number_format($max_train),
	'SOLDIERS'				=> sprintF($user->lang['SOLDIERS_READY'],number_format($kingdom_soldiers)),
	)
);

// Output page
page_header($game_name.' - '.$user->lang['BARRACKS']);

$template->set_filenames(array(
	'body' => $templates_path.'barracks_body.html')
);

page_footer();
?>